import isArray from 'lodash/isArray'
import { ElMessage } from 'element-plus'
import trim from 'lodash/trim'
import { translateTitle } from '@/utils/i18n'
//必须带done回调 执行完成调用
export default function pdaGetFormData({ vm, action, done, eventName }) {
  if (action.extension.formSelect.value.length > 0) {
    // console.log(action.extension.formSelect)
    let formDatas = {}
    let self = null

    action.extension.formSelect.value.forEach((item) => {
      // console.log(vm.vuiComponents[item])
      self = vm.vuiComponents[item]?.self
      if (self) {
        if (self.getInfo.type == 'uiCirculationForm') {
          formDatas = self.getInfo.data.requestListData
        } else if (isArray(self.getInfo.data.formData)) {
          formDatas = self.getInfo.data.formData
        } else {
          let formData = {}
          const getDataKey = action.extension.getDataKey.value
          const injectDataKey = (action.extension.injectDataKey && action.extension.injectDataKey.value) || ''
          if (!trim(getDataKey)) {
            // 如果没有获取数据Key 则返回全量数据
            // dataObj = cloneDeep(callbackDataObj)
            formData = self.getInfo.data.formData
          } else {
            const getDataKeyList = getDataKey.split(',')

            if (!injectDataKey) {
              // 如果没有注入数据 key 则返回获取数据key
              getDataKeyList.map((item) => {
                item = trim(item)
                formData[item] = self.getInfo.data.formData[item]
              })
            } else {
              // 如果有注入数据 key
              const injectDataKeyList = injectDataKey.split(',')
              getDataKeyList.map((item, index) => {
                item = trim(item)
                // 有注入数据 key 则用注入数据 key, 没有则用获取数据 key
                const dataKey = injectDataKeyList[index] || item
                formData[dataKey] = self.getInfo.data.formData[item]
              })
            }
          }

          // 通过获取数据key筛选数据----Nico隐藏之前的拿值逻辑
          // if (action.extension.getDataKey && action.extension.getDataKey.value) {
          //   action.extension.getDataKey.value.split(',').forEach((keyItem) => {
          //     keyItem = trim(keyItem)
          //     formData[keyItem] = self.getInfo.data.formData[keyItem]
          //   })
          // } else {
          //   formData = self.getInfo.data.formData
          // }
          formDatas = Object.assign(formDatas, formData)
          // // 合并老值
          // if (action.extension.getOldValueKey && action.extension.getOldValueKey.value) {
          //   action.extension.getOldValueKey.value.split(',').forEach((keyItem) => {
          //     keyItem = trim(keyItem)
          //     if (self.getInfo.data._formData && self.getInfo.data._formData[keyItem] !== undefined) {
          //       formDatas[`old_${keyItem}`] = self.getInfo.data._formData[keyItem]
          //     }
          //   })
          // }
        }
      }
    })
    // 存储动作数据
    vm.$store.dispatch('actionDatas/setActionsData', {
      moduleId: vm.info._id,
      actionId: action._id,
      actionData: formDatas,
    })

    if (self) {
      done(null, formDatas)
    } else {
      ElMessage({
        showClose: true,
        message: translateTitle('Pda.elements.获取表单值提示'),
        type: 'error',
      })
      // 修改任务队列执行状态，防止下一次事件被拦截
      vm[`${eventName}IsRunning`] = false
    }
  } else {
    // 存储动作数据
    vm.$store.dispatch('actionDatas/setActionsData', {
      moduleId: vm.info._id,
      actionId: action._id,
      actionData: {},
    })
    done(null, {})
  }
}
